<template>
  <div class="content" ref="content">
    <!-- {{ tableData }} -->
    <div class="table-default" v-if="orderData">
      <el-button type="primary" class="backBtn" @click="backHandler">返回</el-button>
      <el-button type="warning" class="backBtn" @click="approveHandler(route.query.id)" v-if="orderData.status != '1'" v-loading.fullscreen.lock="fullscreenLoading"
        ><Icon name="wrench-screwdriver" />审核</el-button
      >
      <el-button type="success" @click="editClick" v-if="orderData.status != '1'">保存修改</el-button>
      <div id="base" ref="base">
        <el-descriptions :column="4" border title="基本信息">
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="盘点单号">{{ orderData.docno }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="组织代码">{{ orderData.org_code }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="库位代码">{{ orderData.location }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="状态">{{ DocStatus[orderData.status]  }}</el-descriptions-item>
          <!-- <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="备注">{{ orderData.remarks }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="回写状态">{{ orderData.writestate }}</el-descriptions-item> -->
        </el-descriptions>
      </div>

      <div id="details" ref="details">
        <el-tabs v-model="activeName" class="demo-tabs">
          <el-tab-pane label="盘点单-明细" name="details">
            <el-table :data="tableData" class="mt-3" border v-loading="loading">
              <el-table-column prop="material.fnumber" label="品号" min-width="150" />
              <el-table-column prop="material.fname" label="品名" min-width="200" />
              <el-table-column prop="material.fspecification" label="规格" min-width="200" />
              <el-table-column prop="material.funit_name" label="单位" min-width="80" />
              <el-table-column prop="lot_num" label="箱号" min-width="200" />
              <el-table-column prop="qty" label="库存数量" min-width="130">
                <template #default="scope">
                {{ Number(scope.row.qty) }}
                </template>
              </el-table-column>
              <el-table-column prop="stock_qty" label="实盘数量" min-width="180">
                <template #default="scope">
                  <el-input-number v-model="scope.row.stock_qty" :min="0" />
                </template>
              </el-table-column>
              <el-table-column prop="wms.fnumber" label="仓库编码" min-width="130" />
              <el-table-column prop="wms.fname" label="仓库名称" min-width="130" />
              <el-table-column prop="location" label="库位代码" min-width="130" />
              <el-table-column prop="locations.warehousecode" label="库位名称" min-width="130" />
              <el-table-column prop="caigoucreator_id" label="创建者" min-width="130" />
            </el-table>
            <Paginate />
          </el-tab-pane>
          <el-tab-pane label="盘点单-盘盈" name="gain">
            <el-table :data="gainList" class="mt-3" border v-loading="loading">
              <el-table-column prop="material.fnumber" label="品号" min-width="150" />
              <el-table-column prop="material.fname" label="品名" min-width="200" />
              <el-table-column prop="material.fspecification" label="规格" min-width="200" />
              <el-table-column prop="material.funit_name" label="单位" min-width="80" />
              <el-table-column prop="lot_num" label="箱号" min-width="200" />
              <el-table-column prop="qty" label="库存数量" min-width="130">
                <template #default="scope">
                {{ Number(scope.row.qty) }}
                </template>
              </el-table-column>
              <el-table-column prop="stock_qty" label="实盘数量" min-width="180">
                <template #default="scope">
                  <el-input-number v-model="scope.row.stock_qty" :min="0" />
                </template>
              </el-table-column>
              <el-table-column prop="wms.fnumber" label="仓库编码" min-width="130" />
              <el-table-column prop="wms.fname" label="仓库名称" min-width="130" />
              <el-table-column prop="location" label="库位代码" min-width="130" />
              <el-table-column prop="locations.warehousecode" label="库位名称" min-width="130" />
              <el-table-column prop="caigoucreator_id" label="创建者" min-width="130" />
            </el-table>
            <!-- <Paginate /> -->
          </el-tab-pane>
          <el-tab-pane label="盘点单-盘亏" name="loss">
            <el-table :data="lossList" class="mt-3" border v-loading="loading">
              <el-table-column prop="material.fnumber" label="品号" min-width="150" />
              <el-table-column prop="material.fname" label="品名" min-width="200" />
              <el-table-column prop="material.fspecification" label="规格" min-width="200" />
              <el-table-column prop="material.funit_name" label="单位" min-width="80" />
              <el-table-column prop="lot_num" label="箱号" min-width="200" />
              <el-table-column prop="qty" label="库存数量" min-width="130">
                <template #default="scope">
                {{ Number(scope.row.qty) }}
                </template>
              </el-table-column>
              <el-table-column prop="stock_qty" label="实盘数量" min-width="180">
                <template #default="scope">
                  <el-input-number v-model="scope.row.stock_qty" :min="0" />
                </template>
              </el-table-column>
              <el-table-column prop="wms.fnumber" label="仓库编码" min-width="130" />
              <el-table-column prop="wms.fname" label="仓库名称" min-width="130" />
              <el-table-column prop="location" label="库位代码" min-width="130" />
              <el-table-column prop="locations.warehousecode" label="库位名称" min-width="130" />
              <el-table-column prop="caigoucreator_id" label="创建者" min-width="130" />
            </el-table>
            <!-- <Paginate /> -->
          </el-tab-pane>
        </el-tabs>
      </div>

    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import { useRouter, useRoute } from 'vue-router'
import detailTableVue from './detaiTable.vue'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import storage from '/admin/utils/storage'
import { delDetailRouter } from '/admin/utils/delDetailRouter'

const router = useRouter()
const route = useRoute()

const api = `inventory/inventory/order/detail?order_id=${route.query.id}`
// const api = `inventory/inventory/order/detail/${route.query.id}`
const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

const tabName = ref('')
const gainList = ref([])
const lossList = ref([])

console.log(tableData)

const summaryData = ref([])

let receiptType = ref(storage.getItem('receiptType') || [])
console.log(receiptType.value)

let receiptTypeFormatter = reactive({})

receiptType.value.forEach(item => {
  receiptTypeFormatter[item.value] = item.label
})

console.log(receiptTypeFormatter)


const fullscreenLoading = ref(false)

const approveHandler = id => {
  // editClick()
  fullscreenLoading.value = true

  http
    .put(`/takestock/interface/${id}`)
    .then(r => {
      fullscreenLoading.value = false

      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}

// 请求盘盈
const getgainList = () => {
  // fullscreenLoading.value = true
  http
    .get(`inventory/inventory/order/detail?order_id=${route.query.id}&gain_loss=1`)
    .then(r => {
      fullscreenLoading.value = false
      console.log('盘盈数据',r);
      gainList.value = r.data.data
    })
    .catch(error => {
      fullscreenLoading.value = false
    })

    http
    .get(`inventory/inventory/order/detail?order_id=${route.query.id}&gain_loss=2`)
    .then(r => {
      fullscreenLoading.value = false
      console.log('盘亏数据',r);
      lossList.value = r.data.data

    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}

getgainList()

let DocStatus = reactive({})


let getDocStatus = () => {
  http.get(`options/DocStatus`).then(r => {
    r.data.data.forEach((item, index) => {
      DocStatus[item.value] = item.label
    })
    console.log('单据状态', DocStatus)
  })
}

getDocStatus()

// 修改
// 修改接口还没出
const editClick = () => {
  let detailObj = tableData.value.detailscheck_all.map(item => {
    return {
      // id: Number(route.query.id),
      quantity: item.quantity,
    }
  })
  console.log('详情修改数据：', detailObj[0])

  updateDetail(detailObj[0])

}
const updateDetail = data => {
  http.put(`/intelligent/Modifyinterface/${route.query.id}`, data).then(r => {
    console.log(r.data.data)
    let res = r.data.data
    if ((res.code = '1000')) {
      ElMessage({
        message: res.message,
        type: 'success',
      })
    } else {
      ElMessage({
        message: res.message,
        type: 'error',
      })
    }
  })
}

const writestateFormatter = ref({
  0: '回写失败',
  1: '回写成功',
})

const searchbox = ref(null)
const showRef = ref(null)
const flag = ref(false)

console.log('路由参数接收', route.query.id)

const backHandler = () => {
  delDetailRouter('盘点单详情', '/inventory/inventoryOrder')
}
const isMore = ref<boolean>(false)

const activeName = ref('details')
const base = ref(null)
const details = ref(null)
const content = ref()
const currentId = route.query.id
let orderData = ref({})
const getorderDetais = () => {
  http.get(`/inventory/inventory/order/list/${route.query.id}`).then(r => {
    console.log('单据头数据：',r.data.data);
    orderData.value = r.data.data
    
  })
}
getorderDetais()


onMounted(() => {
  search()
  deleted(reset)
})
</script>

<style lang="css">
/* .content{
  border: 2px solid red;
 overflow:hidden;
} */
.backBtn {
  margin: 20px 10px;
}

h2 {
  font-weight: bold;
}
#base {
  margin-bottom: 30px;
  /* border: 2px solid green; */
}
#details {
  /* border: 2px solid pink; */
}

.tabs {
  padding: 10px 0 0;
  /* border: 2px solid #000; */
  z-index: 999;
  background-color: #fff;
  position: sticky;
  top: -15px;
}

.tagRow {
  display: inline-block;
  /* border: 1px solid #000; */
  margin: 20px 10px;
}

.tagRow ul {
  display: flex;
  border: 1px solid #eee;
}
.tagRow ul li {
  margin: 0 5px;
  /* border: 1px solid #eee; */
  padding: 5px 10px;
  /* border-radius: 5px; */
}
.active {
  background-color: #409eff;
  color: #fff;
}
.labelClassNAme {
  width: 130px;
}
.contentClassName {
  width: 170px;
}
</style>
